package b1.scenes.fight
{
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.scenes.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;
	
	import com.greensock.*;

	public class ManagedIcon extends Sprite
	{
		public var managedFlag:Boolean;
		private var tip:BasicMessage;
		private var iconImage:Sprite;
		
		public function ManagedIcon()
		{
			super();
			//图标
			var IconImage:Class = ResourceHelper.getInstance().getClassByName("BobotIcon");
			iconImage = new IconImage();
			addChild(iconImage);
			managedFlag = true;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.CLICK, onMouseClick);
			//文字
			var titleText:TextField = new TextField();
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
        	titleText.text = "托管";
			titleText.x = (iconImage.width - titleText.width) / 2;
			titleText.y = iconImage.height + 3;
			addChild(titleText);
		}
		
		public function enableManager():void
		{
			managedFlag = true;
			this.filters = null;
			//如果当前动物已到达右侧
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			if (fight.leftAnimal.progressBar.reachRightEdgeFlag)
			{
				fight.leftAnimal.actionByBobot();
				MagicPane.getInstance().close();
			}
		} 
		
		public function disableManager():void
		{
			managedFlag = false;	
			this.filters = [new ColorMatrixFilter(BFont.blackWhiteFilter)];
		}
		
		public function switchStatus():void
		{
			if (managedFlag)
			{
				disableManager();
			}
			else
			{
				enableManager();
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			if (null == tip)
			{
				var tipStr:String;
				 if (this.managedFlag)
				 {
				 	tipStr = "点击取消托管";
				 }
				 else
				 {
				 	tipStr = "点击启用托管";
				 }
				tip = new BasicMessage(tipStr.length * 18, 30, tipStr);
				tip.x = this.x + iconImage.x + (iconImage.width - tip.width) / 2;
				tip.y = this.y + iconImage.y + iconImage.height + 30;
				this.parent.addChild(tip);
			}
			
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			if (null !=  tip && null != tip.parent)
        	{
        		tip.parent.removeChild(tip);
        		tip = null;
        	}
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			if (SceneContainer.getInstance().mouseDisableFlag && !SceneContainer.getInstance().checkOnlyMousePosition(event.currentTarget))
				return;
			switchStatus();
			//添加新手导引
			if (SceneContainer.getInstance().step == 19 || SceneContainer.getInstance().step == 26)
			{
				SceneContainer.getInstance().addStep();
				TweenLite.delayedCall(2, (SceneContainer.getInstance().currentScene as Fight).endGame);
			}
		}
	}
}